package com.hfb.mashibing.alip8.designpattern._013Iterator;

import java.util.Objects;

/**
 * Q: 构架一个容器，可以添加对象
 * A: 数组实现 V1
 */
public class V1Main {

    public static void main(String[] args) {
        ArrayList li = new ArrayList();
        for (int i = 0; i < 15; i++) {
            li.add(new String("s" + i));
        }
        System.err.println(li.size());
    }

    /**
     * 相比数组，此容器不用考虑边界问题，可以动态扩展
     */
    static class ArrayList {
        Object[] objects = new Object[10];
        /*
         *   objects 中下一个空位置在哪儿，or 目前容器中有多少个元素
         */
        private int index = 0;   // 冗余数据

        public void add(Object o) {
            if (index == objects.length) {
                Objects[] newObjects = new Objects[objects.length*2];
                System.arraycopy(objects, 0, newObjects, 0, objects.length);
                objects = newObjects;
            }
            objects[index] = o;
            index ++;
        }
        public int size(){
            return index;
        }
    }
}
